package com.ruoyi.system.demo;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.read.listener.PageReadListener;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.LogisticsInfo;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author：zhanghao
 * @name：ExcelTest
 * @Date：2025/8/22 14:10
 */
public class ExcelTest {
    public static void main(String[] args) {
        String fileName = "E:\\home\\ruoyi\\物流统计.xlsx";
        List<LogisticsInfo> resultList = new ArrayList<>();
        EasyExcel.read(fileName, LogisticsInfo.class, new PageReadListener<LogisticsInfo>(dataList -> {
            for (LogisticsInfo demoData : dataList) {
//                System.out.println("demoData=" + JSON.toJSONString(demoData));

                if (StringUtils.isNotEmpty(demoData.getUnload())) {
                    String[] unloadArr = demoData.getUnload().split("到");
                    String unload = unloadArr.length > 1 ? unloadArr[1] : unloadArr[0];
                    String result = PythonTest.runPythonScript(unload);
//                    System.out.println("result=" + result);
                    JSONArray jsonArray = JSON.parseArray(result);
//                    System.out.println("jsonArray=" + jsonArray);
                    if (jsonArray.size() > 0) {
                        JSONObject jsonObject = (JSONObject) jsonArray.get(0);
                        demoData.setSheng(jsonObject.getString("省"));
                        demoData.setShi(jsonObject.getString("市"));
                        demoData.setQu(jsonObject.getString("区"));
                        demoData.setAddres(jsonObject.getString("地址"));
                    }
                }

                System.out.println(JSON.toJSONString(demoData));
                resultList.add(demoData);
            }
        })).sheet(6).doRead();


//        for (LogisticsInfo demoData : resultList) {
//            System.out.println(JSON.toJSONString(demoData));
//        }


        fileName = "E:\\home\\ruoyi\\" + System.currentTimeMillis() + ".xlsx";
        // 这里 需要指定写用哪个class去写，然后写到第一个sheet，名字为模板 然后文件流会自动关闭
        // 如果这里想使用03 则 传入excelType参数即可
        EasyExcel.write(fileName, LogisticsInfo.class).sheet("模板").doWrite(resultList);

    }
}
